

import flash.data.SQLConnection;
import flash.data.SQLResult;
import flash.data.SQLStatement;
import flash.events.MouseEvent;
import flash.events.SQLErrorEvent;
import flash.events.SQLEvent;
import flash.filesystem.File;
import flash.utils.setTimeout;

import mx.collections.ArrayCollection;
import mx.collections.ArrayList;
import mx.collections.IList;
import mx.events.StateChangeEvent;

import LocalNotificationInfo;

public var notificationDB:SQLConnection;
public var notificationDBStatement:SQLStatement;
[Bindable] public var notificationCollection:ArrayCollection = new ArrayCollection();

private function initNotificationDatabase():void
{
	notificationDB = new SQLConnection();
	var dbFile:File = File.applicationStorageDirectory.resolvePath("notification.db");
	notificationDB.open(dbFile);
	notificationDBStatement = new SQLStatement();
	notificationDBStatement.sqlConnection = notificationDB;
	
	notificationDBStatement.itemClass = LocalNotificationInfo;
	notificationDBStatement.addEventListener(SQLEvent.RESULT, handleNotiResult);
	//dbStatement.addEventListener(SQLErrorEvent.ERROR, createError);
	
	trace("db initing...");
	
	notificationDBStatement.text = "CREATE TABLE IF NOT EXISTS notifications ( no INTEGER PRIMARY KEY AUTOINCREMENT, lastNo INTEGER)";
	notificationDBStatement.execute();
	
}

private function handleNotiResult(event:SQLEvent):void
{
	var result:SQLResult = notificationDBStatement.getResult();
	if (result != null) {  
		notificationCollection = new ArrayCollection(result.data);
		try{
			if(!isChkedLocalNotiInfo){
				lastNotiNum = notificationCollection[0].lastNo;
				trace("notiDBLength : " + notificationCollection.length);
				trace("notiDBFirstRowIDX : " + notificationCollection[0].no);
				isChkedLocalNotiInfo = true;
			}
			trace("bringed lastNo is : " + lastNotiNum);
		}catch(e:Error){
			lastNotiNum = 0;
		}
	}
	
	// trace("handled");
	
}



private function getNotifications():void
{
	trace("db getting..");
	//notificationDBStatement.text = "SELECT * from notifications WHERE no = '1'";
	notificationDBStatement.text = "SELECT * from notifications";
	notificationDBStatement.execute();
}

private function onSaveNotification():void{
	
	var notification:LocalNotificationInfo = new LocalNotificationInfo();
	
	delNotification();
	//expireLocalNotiStatus();
	
	notification.lastNo = lastNotiNum;
	trace("lastNo : " + lastNotiNum);
	addNotification(notification);
}


private function addNotification(notification:LocalNotificationInfo):void
{
	
	try{
		notificationDBStatement.text = "INSERT INTO notifications (lastNo) values ('" + notification.lastNo + "') ";
	}catch(e:Error){trace(e.toString());}
	
	trace("adding lastNotiNo = " + notification.lastNo);
	
	lastNotiNum = notification.lastNo;
	
	notificationDBStatement.execute();
	
	getNotifications();
}

private function delNotification():void
{
	trace("db deleting");
	var sqlDelete:String = "delete from notifications";
	notificationDBStatement.text = sqlDelete;
	notificationDBStatement.execute();
	//
	
	//lastNotiNum = 0;
}
